package com.tientui.app;

import java.net.URI;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Bitmap;
import android.net.MailTo;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.tientui.app.widgets.WGFWebView;

public class WGFWebViewClient extends WebViewClient {
	private static String TAG = "WGFWebViewClient";

	private MainActivity activity;
	private WGFWebView webView;
	public static String appVersion;
	public static String lastUrl = "";
	public static boolean isError = false;
		
	 @Override
     public void onLoadResource(WebView pWebView, String pUrl) {
     	
		 if (AppConfig.DEBUG)  Log.e("WebView", "onLoadResource: " + pUrl);
     }
	
	
	public WGFWebViewClient(MainActivity activity){
		super();
		this.activity = activity;
		loadAppVersion();
		
	}
	
	@Override
	public void onPageStarted(WebView view, String url, Bitmap favicon) {
		
		if (AppConfig.DEBUG) Log.v(TAG, "onPageStarted: " + url);

	}
	
	@Override
	public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
	{
		isError = true;
		view.loadUrl("file:///android_asset/html/time_out_popup.html");
	}

	@Override
	public void onPageFinished(WebView view, String url) {
		if (AppConfig.DEBUG) Log.v("URL", "--------finish page: " + url);

	}   

	
	@Override
 	public boolean shouldOverrideUrlLoading(WebView view, String url) {
		if (AppConfig.DEBUG) Log.v("LOAD URL", "------- shouldOverrideUrlLoading: " + url);
		
		return false;
	}
	
	private void loadAppVersion(){
		try {
			String packageName = activity.getPackageName();
			PackageInfo packageInfo = activity.getPackageManager().getPackageInfo(packageName, PackageManager.GET_META_DATA);
			appVersion = packageInfo.versionName;
			
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
	}
}
